<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>12 受控组件</title>
</head>

<body>
    <div id="root"></div>
</body>

<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/prop-types@15.8.1/prop-types.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>

<script type="text/babel">
    class App extends React.Component {
        constructor(props) {
            super(props)
        }

        state = {}

        handleSubmit = (event) => {
            console.log(this.state)
        }

        handleChange = (event) => {
            const { name, value } = event.target
            this.setState({ [name]: value })
        }

        render() {
            const { } = this.state

            return (
                <form onSubmit={this.handleSubmit}>
                    <pre>
                        <code>{JSON.stringify(this.state, null, 2)}</code>
                    </pre>
                    <label htmlFor="username">
                        <input onChange={this.handleChange} name="username" id="username" type="text" placeholder="用户名" />
                    </label>

                    <label htmlFor="password">
                        <input onChange={this.handleChange} id="password" name="password" type="password" placeholder="密码" />
                    </label>

                    <button>登录</button>
                </form>
            )
        }
    }

    const root = ReactDOM.createRoot(document.getElementById('root'))
    root.render(<App />)
</script>

</html>